package org.aeon.client.handler.Inbound;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.aeon.client.context.GlobalContext;
import org.aeon.message.response.LoginResponseMessage;

import static org.aeon.client.context.MessageMap.MessageContextKey.LoginMessage;


/**
 * @author: MissingAeon
 * @date: 2024/1/26 12:34
 * @description: 登录响应消息处理程序
 */
@Slf4j
@ChannelHandler.Sharable
public class LoginResponseHandler extends SimpleChannelInboundHandler<LoginResponseMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponseMessage msg) throws Exception {
        GlobalContext.getContext().getMessageMap().set(LoginMessage, msg.getReason());

        final GlobalContext context = GlobalContext.getContext();
        context.getIsLogin().set(msg.isSuccess());
        if (msg.isSuccess()) {
            // 登陆成功
            context.setToken(msg.getToken());
            context.setCurrentUser(msg.getUser());
        }
        ctx.fireChannelRead(msg);
    }
}
